package com.keeping.aiimagery.controller;


import com.alipay.api.AlipayApiException;
import com.keeping.aiimagery.entity.Alipay.OrderDTO;
import com.keeping.aiimagery.entity.Alipay.ResponseDTO;
import com.keeping.aiimagery.entity.OrdersGoods;
import com.keeping.aiimagery.entity.QueryVo;
import com.keeping.aiimagery.entity.Untils.SignVerify;
import com.keeping.aiimagery.entity.UpdataVo;
import com.keeping.aiimagery.mapper.UserMapper;
import com.keeping.aiimagery.service.IStoreService;
import com.keeping.aiimagery.service.InviteService;
import com.keeping.aiimagery.service.OrdersGoodsService;
import com.keeping.aiimagery.service.UserService;
import com.keeping.aiimagery.service.impl.StoreServiceImpl;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * <p>
 * 充值记录 前端控制器
 * </p>
 *
 * @author keeping
 * @since 2023-04-05
 */
@Controller
@RequestMapping("/ordersGoods")
public class OrdersGoodsController {
    @Resource
    private OrdersGoodsService ordersGoodsService;

    @Resource
    private StoreServiceImpl storeService;

    @Resource
    private UserService userService;

    @Resource
    private InviteService inviteService;

    @PostMapping("/aliPay")
    @ResponseBody
    public ResponseDTO<String> aliPayOrder(@RequestBody OrderDTO orderDTO){
        return storeService.aliPayOrder(orderDTO);
    }
    //异步验签的通知，进行数据库操作
    @PostMapping("/notifyUrl")
    @ResponseBody
    public String notifyer(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException, IOException {
        OrdersGoods result= SignVerify.notifyVerfied(request);
        if(result!=null){
            System.out.println("异步验签成功");


            int price = result.getPrice();
            int priceTrue = price/10;

            int idFalse = result.getUserId();
            Integer idTrue = null;
            if (inviteService.getInviteId(idFalse)!=null)
            {
                idTrue = inviteService.getInviteId(idFalse);
                UpdataVo ur = new UpdataVo();
                ur.setIntegral(priceTrue);
                ur.setId(idTrue);
                userService.updataById(ur);
            }

            //防止重复插入
            if(ordersGoodsService.findByTradeNo(result.getTradeNo())==null)
            {
                ordersGoodsService.insert(result);

            }
            Integer num=result.getIntegral();
            Integer id=result.getUserId();
            UpdataVo u=new UpdataVo();
            u.setIntegral(num);
            u.setId(id);
            userService.updataById(u);
            return "success";
        }
        else {
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.println("验签失败");
            System.out.println("异步失败");
            return "failed";
        }
    }
    //同步验签和回调
    @RequestMapping(value="/returnUrl")
    @ResponseBody
    public void returner(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException, IOException {


      //  String returnUrl = "http://localhost:8081/user/person/wallet/record";

      //  String returnUrl = "http://localhost:8083/user/person/wallet/record";


        String returnUrl = "http://localhost/user/person/wallet/record";

        //如果验签成功，返回交易信息，否则放回空
        if(SignVerify.returnVerfied(request)){
            System.out.println("同步验签成功");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            //防止重复插入
            out.print("<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/><title>支付结果</title></head><body>");
            out.print("<h1>支付成功，请返回商户页面</h1>");
            out.print("<script type='text/javascript'>window.location.href='" + returnUrl + "'</script>");
            out.print("</body></html>");
        }
        else {
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.println("验签失败");
            System.out.println("同步失败");
        }
    }
    //返回用户的充值记录总数
    @GetMapping("/countById")
    @ResponseBody
    public int coutById(@RequestParam int userId){
        return ordersGoodsService.countById(userId);
    }

    //消费记录分页查询
    @PostMapping("/pageSelectById")
    @ResponseBody
    public List<OrdersGoods> pageSelectById(@RequestBody QueryVo queryVo){
        return ordersGoodsService.selectPageById(queryVo);
    }



}

